package com.qk.management.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.entity.Role;
import com.qk.management.mapper.RoleMapper;
import com.qk.management.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @文件名: RoleServiceImpl
 * @作者: DX
 * @创建日期: 2025/10/8
 * @描述:
 */

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public Role getById(Integer id) {
        return roleMapper.getById(id);
    }

    @Override
    public void update(Role role) {
        if (role.getId() == null || role.getName() == null || role.getLabel() == null || role.getName().isEmpty() || role.getLabel().isEmpty()){
            CommonException.thowsCommonException(ParamEnum.PARAM_ENUM);
        }
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.update(role);
    }

    @Override
    public void add(Role role) {
        if (role.getName() == null || role.getLabel() == null || role.getName().isEmpty() || role.getLabel().isEmpty()){
            CommonException.thowsCommonException(ParamEnum.PARAM_ENUM);
        }
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.add(role);
    }



    @Override
    public void deleteById(Integer id) {
        roleMapper.delete(id);
    }

    @Override
    public List<Role> listAll() {
        return roleMapper.listAll();
    }

    @Override
    public PageResult<Role> page(String name, String label, Integer page, Integer pageSize) {
        if (page == null || pageSize == null){
            CommonException.thowsCommonException(ParamEnum.PARAM_ENUM);
        }
        PageHelper.startPage(page, pageSize);

        List<Role> pageResult = roleMapper.list(name, label);

        Page<Role> pageInfo = (Page< Role>)pageResult;

        return PageResult.<Role>builder()
                .total(Long.valueOf(pageInfo.getTotal()).intValue())
                .rows(pageInfo.getResult())
                .build();
    }
}
